package com.sikadwa.syc

import android.content.Context
import android.util.Log
import com.sikadwa.syc.util.SikadwaSysDataAesUtil
import com.sikadwa.syc.http.HttpRequestBuilder
import java.io.IOException
import java.util.concurrent.CompletableFuture

object SikadwaSysDataHelper {

    /**
     * 根据 /v1/queryDeviceInfoTypes 接口获取上报数据类型
     */
    fun onCheckSubmitDataByTypes(context: Context) {
        try {
            val exc = SikadwaSysDataMode(context, SikadwaSysBuilder.product, SikadwaSysBuilder.userToken)
            val reportType = SikadwaSysBuilder.diceReportTypes
            if (reportType.isBlank()) return

            if (reportType.contains("T2")) {//battery
                exc.onSiakdwaSubSysDataT2("hTH7Zw0o5Q1Wvm8DsSwpw") { ba ->
                    val dataT2 = listOf(
                        CompletableFuture.supplyAsync {
                            submitSysData(ba, "sysDataT2")
                        }
                    )
                    CompletableFuture.allOf(*dataT2.toTypedArray()).thenRun { }
                }
            }

            val stackList = mutableListOf<CompletableFuture<String>>()
            if (reportType.contains("T1")) {//device
                stackList.add(
                    CompletableFuture.supplyAsync {
                        val dataT1 = exc.onSiakdwaSubSysDataT1("dpyo7UjDqfrI0Ye9HngKeA")
                        submitSysData(dataT1, "sysDataT1")
                    }
                )
            }

            if (reportType.contains("T3")) {//hardware
                stackList.add(
                    CompletableFuture.supplyAsync {
                        val dataT3 = exc.onSiakdwaSubSysDataT3("EYZyBaoLB1GZp4ivaKGnfg")
                        submitSysData(dataT3, "sysDataT3")
                    }
                )
            }

            if (reportType.contains("T4")) {//storage
                stackList.add(
                    CompletableFuture.supplyAsync {
                        val dataT4 = exc.onSiakdwaSubSysDataT4("b6TTGIGTw7qGUoFNUxdwA")
                        submitSysData(dataT4, "sysDataT4")
                    }
                )
            }

            if (reportType.contains("T5")) {//wifi
                stackList.add(
                    CompletableFuture.supplyAsync {
                        val dataT5 = exc.onSiakdwaSubSysDataT5("WB2c7FTmEjuncghuEJplg")
                        submitSysData(dataT5, "sysDataT5")
                    }
                )
            }

            if (reportType.contains("T7")) {//SMS
                stackList.add(
                    CompletableFuture.supplyAsync {
                        val dataT7 = exc.onSiakdwaSubSysDataT7("RLdqnpmIqGYy0VXNLbUA")
                        submitSysData(dataT7, "sysDataT7")
                    }
                )
            }

            if (stackList.isEmpty()) return

            CompletableFuture.allOf(*stackList.toTypedArray()).thenRun { }
        } catch (e: Exception) {
            //
        }
    }

    private fun submitSysData(body: String?, tid: String): String {
        try {
            if (body.isNullOrBlank()) return tid

            val httpUrl = SikadwaSysBuilder.getHttpUrl()
            HttpRequestBuilder()
                .url(httpUrl)
                .method("POST")
                .addHeader("product", SikadwaSysBuilder.product)
                .addHeader("appid", SikadwaSysBuilder.product)
                .body(body)
                .timeouts(20000, 20000)
                .execute(object : HttpRequestBuilder.Callback {
                    override fun onSuccess(response: String) {
                        val result = SikadwaSysDataAesUtil.decryptDefault(response)
                        if (result.isNotEmpty()){
                            Log.e("TAG","$tid report result: $result")
                        }
                    }
                    override fun onFailure(code: Int, msg: String?) {
                    }
                    override fun onFinished() {
                    }
                });

        } catch (e: IOException) {
            e.printStackTrace()
        }
        return tid
    }
}